const mongoose = require('mongoose')

const Image = mongoose.model('Image')
const Comment = mongoose.model('Comment')

const stats = async() =>
    await Promise.all([
        Image.aggregate().group({
            _id:'total',
            images: {$sum: 1},
            views: {$sum: '$views'},
            likes: {$sum: '$likes'}
        }),
        Comment.aggregate().group({
            _id:'total',
            comments: {$sum: 1}
        })
    ])
    .then(([[res],[commentsRes]]) => ({
           images: res  ? res.images : 0,
           views: res  ? res.views : 0,
           likes: res ? res.likes : 0,
           comments: commentsRes ? commentsRes.comments : 0
    }))
    .catch(err => console.log(err))
    
module.exports = stats
